package com.ddtech.platform;

import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import org.json.JSONException;

import java.io.IOException;
import java.util.Map;

public class RVClient  extends AbstractClient{
	private static final String POINT_VALIDATION = "/{id}/validate";	
	/**
	 * 
	 * @param serverUrl
	 */
	public RVClient(String serverUrl){
		super(serverUrl);
	}
	
	
	/**
	 * 跑表达式组
	 * @param packId 表达式组id
	 * @param extraValues 包含所有表达式用到的excel文件的目录
	 * @throws IOException 
	 */
	public void runValidationGroup(String packId, Map<String,String> extraValues) throws ClientException{
				
		try {
			String endPoint = getEndPointUrl(POINT_VALIDATION);
			HttpResponse<JsonNode> ret = Unirest.post(endPoint)
//										.header("Content-Type", "application/json")
										.routeParam("id", packId)
										.field("parameters", toJson(extraValues))
										.asJson();
			int status = ret.getStatus();
			if(status!=200){
				String message = ret.getBody().getObject().getString("message");
				throw new RuntimeException("规则验证失败.返回码："+status+" 出错原因:"+message);
			}
			System.out.println(ret.getBody());
		} catch (UnirestException | JSONException e) {
			throw new ClientException("出错拉。",e);
		}
	}
}
